package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.task.AbTask;
import com.ab.task.AbTaskItem;
import com.ab.task.AbTaskObjectListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.ab.view.titlebar.AbTitleBar;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.SplcLinesChar;
import com.htandroid.htandroid.global.MyApplication;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.webservice.abXMLSplcCharlist;

import java.util.ArrayList;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;


public class Qgddj_detail_return extends AbActivity {
    private MyApplication myApplication; //全局变量application
    private String icvouchid,ztid; //全局变量application
    private EditText rereason_et;
    private Spinner spinner_step;
    private ArrayList<SplcLinesChar> splclineslist;
    private ArrayList<String> list_step = new ArrayList<String>();
    private ArrayAdapter<String> adapter_step;//日期下拉列表
    private AbTitleBar mAbTitleBar = null;
    private Button btn_ref;

    @AbIocView(id = R.id.tv_ok, click = "btnClick")
    TextView tv_ok;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_qgddj_detail_return);
        myApplication = (MyApplication) abApplication;
        initview();
        getdata();
    }

    public void btnClick(View v) {
        btn_ok(v);
    }

    //初始化
    private void initview() {
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("请购单退回");
        mAbTitleBar.setLogo(R.drawable.icon_back);
        mAbTitleBar.getLogoView().setLayoutParams(new LinearLayout.LayoutParams(58, 57));
        mAbTitleBar.getLogoView().setFocusable(true);
        mAbTitleBar.getLogoView().setFocusableInTouchMode(true);
        mAbTitleBar.setTitleBarBackground(R.color.common_title);
        mAbTitleBar.clearRightView();
        mAbTitleBar.setTitlePadding(0, 0, 0, 0);
        View rightView = mInflater.inflate(R.layout.ref, null);
        mAbTitleBar.addRightView(rightView);
        btn_ref = (Button) rightView.findViewById(R.id.icon_ref);
        btn_ref.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getdata();
            }
        });

        //获取传入的数据
        Intent intent = getIntent();// 创建Intent对象
        Bundle bundle = intent.getExtras();// 获取传入的数据，并使用Bundle记录
        icvouchid = bundle.getString("cvouchid");// 获取Bundle中记录的信息
        ztid =  bundle.getString("ztid");
        rereason_et = (EditText) findViewById(R.id.rereason_et);
        spinner_step = (Spinner) findViewById(R.id.spinner_step);
        adapter_step = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_step);
        adapter_step.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_step.setAdapter(adapter_step);
        //设置日期切换监听
        spinner_step.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner mstep = (Spinner) parent;
                //获取账套列表当前选中值,然后刷新年度
                String step_choose = mstep.getSelectedItem().toString();
                if (!splclineslist.isEmpty()) {
                    for (int j = 0; j < splclineslist.size(); j++) {
                        SplcLinesChar splclines = splclineslist.get(j);
                        String displayname = splclines.getDisplayname();
                        //寻找流程所在的列表行
                        if (displayname.equals(step_choose)) {
                            //设置tag
                            spinner_step.setTag(String.valueOf(j));
                            break;
                        }
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public void getdata() {
//        loadingDialog.loading(this);
//        TreeMap<String, String> properties = new TreeMap<String, String>();
//        //参数
//        String[] parms = {"cacc_id", "iyear", "cvouchid"};
//        String[] vals = {(String) myApplication.getGs_ztid(), (String) myApplication.getGs_year(), icvouchid};
//        //生成xml
//        BuildQgdListXMLService service = new BuildQgdListXMLService();
//        String xmlstring = service.buildXML(null, "getqgdsplclines", InstorageURL.NAME_SPACE, parms, vals, null);
//
//        ToolSOAPxml.callService("getqgdsplclines", xmlstring, new ToolSOAPxml.WebServiceCallBack() {
//            @Override
//            public void onSucced(String result) {
//                if (result != null) {
//                    splclineslist = XMLSplcCharlist.paraseCommentInfors(result);
//                    for (int i = 0; i < splclineslist.size(); i++) {
//                        SplcLinesChar step = splclineslist.get(i);
//                        //退回时,如果后续步骤为未审批,则不需要显示
//                        if (step.getCchecker().equals("")) {
//                            break;
//                        }
//                        list_step.add(step.getDisplayname());
//                    }
//                    //使用这个函数直接根据返回结果刷新列表
//                    adapter_step.notifyDataSetChanged();
//                } else {
//                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                }
//                loadingDialog.loadingfinish();
//            }
//
//            @Override
//            public void onFailure(String result) {
//                loadingDialog.loadingfinish();
//                AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//            }
//        });

        AbTask task = new AbTask();
        final AbTaskItem item = new AbTaskItem();
        item.setListener(new AbTaskObjectListener() {

            @Override
            public <T> void update(T entity) {
                String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
                AbSoapParams params = new AbSoapParams();
                params.put("arg0", ztid);
                params.put("arg1", myApplication.getGs_year());
                params.put("arg2", icvouchid);
                mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "getqgdsplclines", params, new AbSoapListener() {
                    //获取数据成功会调用这里
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        if (content != null) {
                            splclineslist = abXMLSplcCharlist.paraseCommentInfors(content);
                            for (int i = 0; i < splclineslist.size(); i++) {
                                SplcLinesChar step = splclineslist.get(i);
                                //退回时,如果后续步骤为未审批,则不需要显示
                                if (step.getCchecker().equals("")) {
                                    break;
                                }
                                list_step.add(step.getDisplayname());
                            }
                            //使用这个函数直接根据返回结果刷新列表
                            adapter_step.notifyDataSetChanged();
                        } else {
                            AbToastUtil.showToast(Qgddj_detail_return.this, R.string.sqlerrormsg);
                        }
                        AbDialogUtil.removeDialog(Qgddj_detail_return.this);
                    }

                    // 失败，调用
                    @Override
                    public void onFailure(int statusCode, String content,
                                          Throwable error) {
                        AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
                        AbDialogUtil.removeDialog(Qgddj_detail_return.this);
                    }

                    // 开始执行前
                    @Override
                    public void onStart() {
                        //显示进度框
                        //AbDialogUtil.showProgressDialog(Qgddj_detail_return.this, 0, "正在加载...");
                    }

                    // 完成后调用，失败，成功
                    @Override
                    public void onFinish() {
                        //移除进度框
                        AbDialogUtil.removeDialog(Qgddj_detail_return.this);
                    }
                });
            }

            @SuppressWarnings("unchecked")
            @Override
            public Integer getObject() {
                return -1;
            }
        });
        task.execute(item);
    }

    public void btn_ok(View v) {
        //必须输入请购原因
        String note = rereason_et.getText().toString();
        if (note == null || note.equals("")) {
            AbToastUtil.showToastInThread(getApplicationContext(), "退回时必须输入退回原因！");
            return;
        }
        int splcrow = 0;
        Intent intent = new Intent();
        if (spinner_step.getTag() != null && !spinner_step.getTag().equals("") && splclineslist != null) {
            splcrow = Integer.valueOf(spinner_step.getTag().toString());
            SplcLinesChar splclines = splclineslist.get(splcrow);
            intent.putExtra("recharname", splclines.getCharname());
            intent.putExtra("restep", splclines.getInorder());
            intent.putExtra("renote", note);
        } else {
            intent.putExtra("recharname", "");
            intent.putExtra("restep", "");
            intent.putExtra("renote", "");
        }
        setResult(R.layout.layout_qgddj_detail_return / 400000, intent);  //设置返回结果
        this.finish(); //关闭子窗口，否则数据无法返回
    }
}
